package com.larvae.dao.user.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.larvae.bean.User;
import com.larvae.dao.user.IUserDao;
import com.larvae.factory.SpringBeanFactory;
/**
 * 
 * @author arlord
 * @since 2013/4/20
 */
public class UserDaoImpl extends HibernateDaoSupport implements IUserDao {

	public void addUser(User user) throws SQLException {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().save(user);

	}

	public void deleteUser(User user) throws SQLException {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().delete(user);

	}

	public User findUserById(User user) throws SQLException {
		// TODO Auto-generated method stub
		return (User)this.getHibernateTemplate().get(User.class, user.getId());
	}

	public List<User> queryUser(User user) throws SQLException {
		// TODO Auto-generated method stub
		SessionFactory sessionFactory = (SessionFactory)SpringBeanFactory.getBean("sessionFactory");
		Criteria c = sessionFactory.openSession().createCriteria(User.class);
		c.add(Restrictions.eq("id", user.getId()))
		.add(Restrictions.like("name", user.getName()))
		.add(Restrictions.like("insteresting", user.getInsteresting()));
		@SuppressWarnings("unchecked")
		List<User> list = c.list();
		return list;
	}

	public void modifyUser(User user) throws SQLException {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().update(user);

	}

}
